package com.mhkj.revise.function.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mhkj.common.enums.PlanType;
import com.mhkj.revise.function.repository.FunctionPlanNodeRepository;
import com.mhkj.revise.function.service.FunctionPlanNodeService;
import com.mhkj.revise.plan.entity.Plan;
import com.mhkj.revise.plan.entity.PlanNode;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.organizational.bean.Company;
import com.mics.core.system.organizational.bean.Office;
import com.mics.core.system.organizational.bean.Post;
import com.mics.core.system.organizational.service.OrgService;
import com.mics.core.system.rbac.entity.Dictionary;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 职能计划节点
 * @author mics
 * @date 2018年8月29日
 * @version 1.0
 */
@Service
public class FunctionPlanNodeServiceImpl extends BaseServiceImpl<PlanNode> implements FunctionPlanNodeService{
	@Resource
	private FunctionPlanNodeRepository functionPlanNodeRepository;
	@Resource
	private OrgService orgService;
	@Resource
	private DictionaryService dictionaryService;
	@Resource
	public void setFunctionPlanNodeRepository(
			FunctionPlanNodeRepository functionPlanNodeRepository) {
		super.setBaseRepository(functionPlanNodeRepository);
	}

	@Override
	public Pagination<PlanNode> listByPage(PlanNode planNode, int pageIndex,
			int pageSize) {
		Specification<PlanNode> spec = new Specification<PlanNode> () {  
			private static final long serialVersionUID = -786003485941679959L;
			@Override
			public Predicate toPredicate(Root<PlanNode> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
              List<Predicate> lstPredicates = new ArrayList<Predicate>();
              if(!StringUtils.isEmpty(planNode.getNodeName())){
                  Predicate p1=criteriaBuilder.like(root.get("name").as(String.class), "%"+planNode.getNodeName()+"%");
                  lstPredicates.add(p1);
              }
              if(planNode.getPlan() != null){
                  Join<Object, Object> planType = root.join("plan",JoinType.INNER);
                  Predicate p1=criteriaBuilder.equal(planType.get("id").as(Long.class), planNode.getPlan().getId());
                  lstPredicates.add(p1);
              }
              Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
              return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
	        }
		};
		Page<PlanNode> pageT =  functionPlanNodeRepository.findAll(spec, PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC,"id"));
		Pagination<PlanNode> pagination = new Pagination<PlanNode>();
		pagination.setTotal(functionPlanNodeRepository.count(spec));
		List<PlanNode> plans = pageT.getContent();
		for(PlanNode planNode2 : plans){
			Company company = orgService.getCompanyById(planNode2.getCompanyId());
			planNode2.setCompanyName(company.getCompanyname());
			Office cOffice = orgService.officeInfo(planNode2.getOfficeId());
			planNode2.setOfficeName(cOffice.getName());
			Post post = orgService.getRoleInfo(planNode2.getPostId());
			planNode2.setPostName(post.getName());
		}
		pagination.setData(plans);
		return pagination;
	}

	@Override
	@Transactional
	public void update(long[] ids, String[] beginDates, String[] endDates) throws ParseException {
		Plan plan  = null;
		for(int i = 0 ;i< ids.length;i++){
			PlanNode planNode = getById(ids[i]);
			plan = planNode.getPlan();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			if(beginDates.length > i && StringUtils.hasText(beginDates[i])){
				planNode.setBeginDate(simpleDateFormat.parse(beginDates[i]));
			}
			if(endDates.length > i && StringUtils.hasText(endDates[i])){
				planNode.setEndDate(simpleDateFormat.parse(endDates[i]));
			}
			edit(planNode);
		}		
		PlanNode beginPlanNode = functionPlanNodeRepository.getFristPlanNode(plan.getId());
		PlanNode endPlanNode = functionPlanNodeRepository.getLastPlanNode(plan.getId());
		plan.setBeginDate(beginPlanNode.getBeginDate()); 
		plan.setEndDate(endPlanNode.getEndDate());
	}

	@Override
	public List<PlanNode> listAllChecked() {
		Dictionary planType = dictionaryService.getByCode(PlanType.FUNC_TYPE.getCode());
		return functionPlanNodeRepository.getPlanNodeByPlanType(planType.getId());
	}
	
}
